using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

using QuestFramework;

namespace QuestFramework.UI
{
    public struct WindowTextElement
    {
        private string text;
        private Vector2 textPosition;
        private Color textColor;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public Vector2 TextPosition
        {
            get { return textPosition; }
            set { textPosition = value; }
        }

        public Color TextColor
        {
            get { return textColor; }
            set { textColor = value; }
        }
    }

    public struct WindowTextureElement
    {
        private Texture2D texture;
        private Vector2 texturePosition;
        private Vector2 textureSize;
        private Color textureColor;

        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        public Vector2 TexturePosition
        {
            get { return texturePosition; }
            set { texturePosition = value; }
        }

        public Vector2 TextureSize
        {
            get { return textureSize; }
            set { textureSize = value; }
        }

        public Color TextureColor
        {
            get { return textureColor; }
            set { textureColor = value; }
        }

        public Rectangle TextureRectangle
        {
            get 
            { 
                return new Rectangle((int)texturePosition.X
                    , (int)texturePosition.Y, (int)textureSize.X, (int)textureSize.Y);
            }
        }
    }

    public class Window
    {
        private int viewportWidth;
        private int viewportHeight;
        protected Game parentGame;
        protected SpriteBatch sprites;
        protected Vector2 windowPosition;
        protected Texture2D backgroundTexture;
        protected List<WindowTextElement> windowTextElements;
        protected List<WindowTextureElement> windowTextureElements;
        protected SpriteFont windowFont;
        protected bool visible;

        public Window(Game game)
        {
            parentGame = game;
            viewportWidth = parentGame.GraphicsDevice.Viewport.Width;
            viewportHeight = parentGame.GraphicsDevice.Viewport.Height;
            windowTextElements = new List<WindowTextElement>();
            windowTextureElements = new List<WindowTextureElement>();
            visible = false;
        }

        public virtual void Initialize()
        {
            sprites = new SpriteBatch(parentGame.GraphicsDevice);
        }

        public virtual void Update(GameTime gameTime)
        {
        }

        public virtual void Draw(GameTime gameTime)
        {
            if (visible)
            {
                sprites.Begin();
                sprites.Draw(backgroundTexture, windowPosition, Color.White);

                for (int textToDraw = 0; textToDraw < windowTextElements.Count; textToDraw++)
                {
                    sprites.DrawString(windowFont, windowTextElements[textToDraw].Text
                        , windowTextElements[textToDraw].TextPosition, windowTextElements[textToDraw].TextColor);
                }

                for (int textureToDraw = 0; textureToDraw < windowTextureElements.Count; textureToDraw++)
                {
                    sprites.Draw(windowTextureElements[textureToDraw].Texture
                        , windowTextureElements[textureToDraw].TextureRectangle
                        , windowTextureElements[textureToDraw].TextureColor);
                }

                sprites.End();
            }
        }

        protected SpriteBatch Sprites
        {
            get { return sprites; }
        }

        public Vector2 WindowPosition
        {
            get { return windowPosition; }
            set { windowPosition = value; }
        }

        public Texture2D BackgroundTexture
        {
            get { return backgroundTexture; }
            set { backgroundTexture = value; }
        }

        public SpriteFont WindowFont
        {
            get { return WindowFont; }
            set { windowFont = value; }
        }

        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }
   }
}
